<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/
jimport( 'joomla.application.component.view' );
class JAJobBoardViewJAFieldList extends JAView
{
    /**
     * Display the view
     */
    function display($tmpl = null)
    {
		$task = JRequest::getVar("task", '');
		switch ($task){
			case 'add':
			case 'edit':				
				JAJobBoardViewJAFieldList::displayForm($tmpl);
				break;			
			default:
				JAJobBoardViewJAFieldList::displayListItems($tmpl);
		}
  	}
  	
  	
  	/**
  	* Display List of items
  	*/
  	function displayListItems($tmpl){  		
  		global $mainframe, $option;
  		
  		// available column for new field list item
        $fields = $this->get('FieldList');
        $n = count($fields);
        if($n){
            JToolBarHelper::addNew('add', JText::_('Add Column'));
            JToolBarHelper::editList('edit', JText::_('Edit Column'));
        }
        
        JToolBarHelper::deleteListX(JText::_('Warning! The selected row(s) will be permanently deleted. Do you want to continue?'), 'remove', 'Delete Column');
  		
        $lists = $this->_getViewLists();
		
        // get data items
        $items = &$this->get('Items');        
		
        $pagination = &$this->get('Pagination');
        
        $ordering = true;
        
        $this->assignRef('ordering', $ordering);
        $this->assignRef('lists', $lists);
        $this->assignRef('items', $items);
        $this->assignRef('pagination', $pagination);
        parent::display($tmpl);
  	}
  	/**
  	* Display edit form
  	*/
  	function displayForm($tmpl){  	    
  		global $mainframe, $option;
  		
  		$filter_table	= $mainframe->getUserStateFromRequest( $option .".jafieldlist.filter_table", 'filter_table', 'ja_profiles',	'word' );
  		
  	    $item = &$this->get('Item');  	
  	    
  	    // available column for new field list item
        $fields = $this->get('FieldList');
        $n = count($fields);
        if(!$n && !$item->id){
            $available = false;
        } else {
            if ($item->id)
            {
	        	JToolBarHelper::apply();
	            JToolBarHelper::save();
	            JToolBarHelper::deleteList('Are you sure to delete?');
	            $available = true;               
            }
            else 
            {
            	JToolBarHelper::apply();
	            JToolBarHelper::save();
//	            JToolBarHelper::deleteList('Are you sure to delete?');
	            $available = true;   
            }
        }
        $this->assignRef('available', $available); 
        
       JToolBarHelper::cancel('cancel', JText::_('Cancel'));
		//print_r($fields );exit;
		// Column
    	if(empty($item->id)){
            if($this->available){
                $fieldHTML = array();
		        for($i = 0; $i < $n; $i++){
           	        $fieldHTML[] = JHTML::_('select.option',  $fields[$i]->id, $fields[$i]->label );
		        }
       	        $lists['column'] = JHTML::_('select.genericlist',   $fieldHTML, 'field_id', 'class="inputbox" size="5" style="width:250px" ', 'value', 'text', $item->field_id);	
            } 
        } else {            
        	JRequest::setVar('fid', $item->field_id);
            $field = &$this->get('Field');
            $lists['column'] = "<strong>". $field->label ."</strong> ( ". $field->field_name ." )"; 
        }
			
        // Field Ordering
		if($item->id){
        $query = 'SELECT ordering as value, label as text '
				.' FROM #__ja_form_lists AS fl LEFT JOIN #__ja_form_fields AS ff ON fl.field_id = ff.id '
				." WHERE ff.table_name = '$filter_table'"
				.' ORDER BY ordering';
			$order = JHTML::_('list.genericordering',  $query );
			$ordering = JHTML::_('select.genericlist',   $order, 'ordering', 'class="inputbox" size="1"', 'value', 'text', intval( $item->ordering ) );
		} else {
			$ordering = '<input type="hidden" name="ordering" value="'. $item->ordering .'" />'. JText::_( 'DESCNEWITEMSLAST' );
		}
        $lists['ordering'] = $ordering;
        
        // Viewable by
        $userTypes = JobBoardConstant::getUserType();
        $userTypesHTML[] = JHTML::_('select.option',  "", "" ); 
        foreach($userTypes as $usertype=>$label){
            $userTypesHTML[] = JHTML::_('select.option',  $usertype, $label );
        }
        $lists['usertype'] = JHTML::_('select.genericlist',   $userTypesHTML, 'viewable_by', 'class="inputbox" size="1"', 'value', 'text', $item->viewable_by);
        
		$lists['linked'] = JHTML::_('select.booleanlist', 'linked', '', $item->linked);
		$lists['admin_only'] = JHTML::_('select.booleanlist', 'admin_only', '', $item->admin_only);
		$lists['clean_format'] = JHTML::_('select.booleanlist', 'clean_format', '', $item->clean_format);
		$lists['is_bold'] = JHTML::_('select.booleanlist', 'is_bold', '', $item->is_bold);
		$lists['is_sortable'] = JHTML::_('select.booleanlist', 'is_sortable', '', $item->is_sortable);
		$lists['no_wrap'] = JHTML::_('select.booleanlist', 'no_wrap', '', $item->no_wrap);
		
        $this->assignRef('option', $option);
        $this->assignRef('item', $item);
        $this->assignRef('pagination', $pagination);
        $this->assignRef('lists', $lists);
        
  	    parent::display($tmpl);
	}
	
	function &_getViewLists()
	{
		global $mainframe, $option;

		$filter_table		= $mainframe->getUserStateFromRequest( JBCOMNAME.".fieldlist.filter_table",		'filter_table',		'ja_profiles',	'word' );
		$filter_order		= $mainframe->getUserStateFromRequest( JBCOMNAME.".fieldlist.filter_order",		'filter_order',		'fl.ordering',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( JBCOMNAME.".fieldlist.filter_order_Dir",	'filter_order_Dir',	'ASC',			'word' );
		$search				= $mainframe->getUserStateFromRequest( JBCOMNAME.".fieldlist.search",			'search',			'',				'string' );
		$search				= JString::strtolower( $search );

		// table ordering
		$lists['order_Dir']	= $filter_order_Dir;
		$lists['order']		= $filter_order;
		$lists['option']	= $option;

		// search filter
		$lists['search']= $search;
		
		$tableList = JobBoardConstant::getTableList();
		
        foreach($tableList as $tableName=>$tableLabel){
            $tablesHTML[] = JHTML::_('select.option',  $tableName, JText::_($tableLabel) );
		}
        $lists['tables'] = JHTML::_('select.genericlist',   $tablesHTML, 'filter_table', 'class="inputbox" size="1" onchange="document.adminForm.submit();"', 'value', 'text', $filter_table);
		$this->assign('tableList', $tableList);
		return $lists;
	}
}
?>